<?php

/*
 * Author: Gehad
 */

class ProductreportController extends Zend_Controller_Action {

    public function init() {
        
    }

    /**
     *  Renders the form of searching criteria.
     *  In case that user's input is valid, forward to the action that will retrieve the products.
     */
    public function searchAction() {

        // Creating form object
        $form = new Application_Form_SearchForm();

        // Rendering the form
        $this->view->form = $form;
    }

    public function dataAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->getHelper('viewRenderer')->setNoRender(true);

        // Getting range of paginator
        $ranges = explode(',', substr($_SERVER['HTTP_RANGE'], 6));
        foreach ($ranges as $range) {
            $parts = explode('-', $range);
            $start = $parts[0]; // If this is empty, this should be 0.
            $end = $parts[1]; // If this is empty or greater than than filelength - 1, this should be filelength - 1.
        }

        $productTable = RepProductTable::getInstance();
        $query = $productTable->getProduct($_GET, $start, $end);
        $products = $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY);


        $this->getResponse()
                ->setHttpResponseCode(200)
                ->setHeader("Content-Range", "items " . $start . "-" . $end . "/" . $productTable->returnCount())
                ->appendBody(Zend_Json::encode($products));
    }

}
